package com.xafc.demo.framework.utils;

import cn.hutool.core.util.StrUtil;

import javax.servlet.http.HttpServletRequest;

/**
 * http请求工具类
 */
public class RequestUtils {
	
	// 判断请求来自PC端还是移动端   true为移动端
    public static boolean isMobile(HttpServletRequest request) {
        //android mac os windows phone
    	String userAgent = request.getHeader("user-agent");
        String[] array = new String[] { "android", "mac os", "windows phone" };
        if (userAgent == null) {
            return false;
        }
        userAgent = userAgent.toLowerCase();
        for (String s : array) {
            if (userAgent.indexOf(s) > 0) {
                return true;
            }
        }
        return false;
    }


    /**
     * 判断请求是否是ajax请求
     *
     * @param request http请求
     * @return 是否为Ajax请求
     */
    public static boolean isAjax(HttpServletRequest request) {
        return (!StrUtil.isEmpty(request.getHeader("accept")) && request.getHeader("accept").contains("application/json"))
                || (request.getHeader("X-Requested-With") != null && request.getHeader("X-Requested-With").contains("XMLHttpRequest"))
                || (null != request.getHeader("referer") && StrUtil.contains(request.getHeader("referer"), "swagger-ui.html"));
    }

}
